<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\library\helper;
use app\common\model\shiyao\Company as CompanyModel;
use app\store\model\shiyao\BalanceLog as BalanceLogModel;
use app\common\enum\user\balanceLog\Scene as SceneEnum;
use app\store\service\store\User as StoreUserService;
use think\facade\Db;

/**
 * 申办方模型
 * Class StoreUser
 * @package app\store\model
 */
class Company extends CompanyModel {

	public function getListAll() {
		// 查询模型
		$query = $this->getNewQuery();

		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->select()->toArray();

		return $list;
	}

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '']);
		// 关键词搜索
		!empty($params['search']) && $query->where('company_name', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->paginate(15)->each(function ($item, $key) {
			$item['project_num'] = Db::name('shiyao_project')->where('company_id', $item['company_id'])->where('is_delete', 0)->count();

			return $item;
		});

		return $list;
	}

	/**
	 * 用户充值
	 * @param string $target 充值类型
	 * @param array $data 表单数据
	 * @return bool
	 */
	public function recharge(string $target, array $data) {
		// 当前操作人用户名
		$storeUserName = StoreUserService::getLoginInfo()['user']['user_name'];
		return $this->rechargeToBalance($storeUserName, $data['balance']);

		return false;
	}

	/**
	 * 用户充值：余额
	 * @param string $storeUserName
	 * @param array $data
	 * @return bool
	 */
	private function rechargeToBalance(string $storeUserName, array $data) {
		
		if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) {
			$this->error = '请输入正确的金额';
			return false;
		}
		// 判断充值方式，计算最终金额
		if ($data['mode'] === 'inc') {
			$diffMoney = $data['money'];
		} elseif ($data['mode'] === 'dec') {
			$diffMoney = -$data['money'];
		} else {
			$diffMoney = helper::bcsub($data['money'], $this['balance']);
		}
		// 更新记录
		$this->transaction(function () use ($storeUserName, $data, $diffMoney) {
			// 更新账户余额
			static::setIncBalance((int) $this['company_id'], (float) $diffMoney);
			// 新增余额变动记录
			BalanceLogModel::add(SceneEnum::ADMIN, [
				'company_id' => $this['company_id'],
				'money' => (float) $diffMoney,
				'remark' => $data['remark'],
			], [$storeUserName]);
		});
		return true;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {
		$data['company_name'] = strtolower($data['company_name']);

		$data['store_id'] = self::$storeId;

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}
	public function stop() {
		return $this->save(['status' => 0]);
	}
	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {
		$data['company_name'] = strtolower($data['company_name']);

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {

		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			return $this->save(['is_delete' => 1]);
		});
	}

}
